package com.szxy.utils;

import com.deepoove.poi.data.RowRenderData;
import com.deepoove.poi.data.style.TableStyle;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STJc;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

/**
 * @Author: zwz
 * @Date:2020/7/28 17:28
 * @Description:com.szxy.utls
 * @Version:1.0
 * 生成模拟数据以及将数据渲染到 word 模板上
 **/
public class RenderDataToWordUtil {

    /**
     * 行样式
     */
    public static TableStyle rowStyle = new TableStyle();

    /**
     * 生成模拟数据
     *
     * @return
     */
    public static Map<String, Object> getDataFromDb(int num) {
        // o1 : col_1,col_2,col_3,...,col_n
        Map<String, Object> map = new TreeMap<>((o1, o2) -> {
            String o11 = o1.substring(o1.indexOf("_") + 1);
            String o22 = o2.substring(o2.indexOf("_") + 1);
            return Integer.valueOf(o11).compareTo(Integer.valueOf(o22));
        });
        for (int i = 0; i < num; i++) {
            map.put(String.format("col_%s", i + 1), String.valueOf(i + 1));
        }
        return map;
    }


    /**
     * 私有化构造器
     */
    private RenderDataToWordUtil() {}

    /**
     * 直接填充不放空值
     * @param map
     * @param paramLength
     * @return
     */
    public static List<RowRenderData> getRowRenderData0(Map<String, Object> map, int paramLength) {
        rowStyle.setAlign(STJc.CENTER);

        List<RowRenderData> allSocialList = new ArrayList<>();
        String[] params = new String[paramLength];
        int index = 0;
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            String value = (String) entry.getValue();
            params[index++] = value;

            if (index == paramLength) {
                RowRenderData row = RowRenderData.build(params);
                row.setStyle(rowStyle);
                allSocialList.add(row);
                index = 0;
            }
        }
        return allSocialList;
    }

    /**
     * 直接填充不放空值，第一列放序号
     * @param map
     * @param paramLength
     * @return
     */
    public static List<RowRenderData> getRowRenderData0AndNo(Map<String, Object> map, int paramLength) {
        rowStyle.setAlign(STJc.CENTER);

        List<RowRenderData> allSocialList = new ArrayList<>();
        String[] params = new String[paramLength];
        int index = 1;
        int seriesNum = 1; //序号
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            String value = (String) entry.getValue();
            params[index++] = value;
            if (index == paramLength) {
                // 放入序号
                params[0] = String.valueOf(seriesNum++);
                RowRenderData row = RowRenderData.build(params);
                row.setStyle(rowStyle);
                allSocialList.add(row);
                index = 1;
            }
        }
        return allSocialList;
    }


    /**
     * 前一列放空值
     * @param map
     * @param paramLength
     * @return
     */
    public static List<RowRenderData> getRowRenderData(Map<String, Object> map, int paramLength) {
        List<RowRenderData> allSocialList = new ArrayList<>();
        String[] params = new String[paramLength];
        params[0] = "";
        int index = 1;
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            String key = entry.getKey();
            String value = (String) entry.getValue();
            params[index++] = value;

            if (index == paramLength) {
                RowRenderData row = RowRenderData.build(params);
                allSocialList.add(row);
                index = 1;
            }
        }
        return allSocialList;
    }


    /**
     * 前二列放空值
     * @param map
     * @param paramLength
     * @return
     */
    public static List<RowRenderData> getRowRenderData2(Map<String, Object> map, int paramLength) {
        List<RowRenderData> allSocialList = new ArrayList<>();
        String[] params = new String[paramLength];
        params[0] = "";
        params[1] = "";
        int index = 2;
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            String key = entry.getKey();
            String value = (String) entry.getValue();
            params[index++] = value;

            if (index == paramLength) {
                RowRenderData row = RowRenderData.build(params);
                allSocialList.add(row);
                index = 2;
            }
        }
        return allSocialList;
    }

    /**
     * 前三列放空值
     * @param map
     * @param paramLength
     * @return
     */
    public static List<RowRenderData> getRowRenderData23(Map<String, Object> map, int paramLength) {
        List<RowRenderData> allSocialList = new ArrayList<>();
        String[] params = new String[paramLength];
        params[0] = "";
        params[1] = "";
        params[2] = "";
        int index = 3;
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            String key = entry.getKey();
            String value = (String) entry.getValue();
            params[index++] = value;

            if (index == paramLength) {
                RowRenderData row = RowRenderData.build(params);
                allSocialList.add(row);
                index = 3; // 重置
            }
        }
        return allSocialList;
    }

    /**
     * 特殊处理第一行和第二行,以及最后一行
     * @param map  数据
     * @param paramLength 一行数据的长度
     * @return
     */
    public static List<RowRenderData> getRowRenderData3(Map<String, Object> map, int paramLength) {
        List<RowRenderData> allSocialList = new ArrayList<>();
        String[] params = new String[paramLength];
        params[0] = "";
        params[1] = "";
        int index = 2;
        int count = 0;
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            String key = entry.getKey();
            String value = (String) entry.getValue();
            params[index++] = value;
            count++;

            if(count > 8 && count < 56) {
                params[2] = "";
            }

            if (index == paramLength) {
                RowRenderData row = RowRenderData.build(params);
                allSocialList.add(row);
                if(count == 55) {
                    index = 2;
                }else {
                    index = 3;
                }
            }

        }
        return allSocialList;
    }

    /**
     * 特殊处理第一行， 在第一行中第1 列赋空字符串
     * @param map
     * @param paramLength
     * @return
     */
    public static List<RowRenderData> getRowRenderData4(Map<String, Object> map, int paramLength) {
        List<RowRenderData> allSocialList = new ArrayList<>();
        String[] params = new String[paramLength];
        params[0] = "";
        int index = 1;  // 起始第一行从第2列填值
        int count = 0;
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            count++;
            String key = entry.getKey();
            String value = (String) entry.getValue();
            params[index++] = value;

            if(count == 8){ //从第二行开始，第1,2填空值
                params[1] = "";
            }

            if (index == paramLength) {
                RowRenderData row = RowRenderData.build(params);
                allSocialList.add(row);
                index = 2;
            }

        }
        return allSocialList;
    }

    public static List<RowRenderData> getRowRenderData5(Map<String, Object> map, int paramLength) {

        return null;
    }
}
